<template>
  <div id="loginview">
    <h2 style="margin-bottom: 16px">用户登录</h2>
    <a-form
      style="max-width: 480px; margin: auto"
      label-align="left"
      auto-label-width
      :model="loginRequest"
      @submit="handleLogin"
    >
      <a-form-item field="userAccount" label="账号">
        <a-input
          v-model="loginRequest.userAccount"
          placeholder="请输入您的账号..."
        />
      </a-form-item>
      <a-form-item
        field="password"
        label="密码"
        tooltip="密码长度不少于8位数字"
      >
        <a-input-password
          v-model="loginRequest.password"
          placeholder="请输入密码..."
        />
      </a-form-item>
      <a-form-item field="userRole" label="请选择身份：">
        <a-radio-group v-model="loginRequest.userRole">
          <a-radio value="3">学生</a-radio>
          <a-radio value="2">教师</a-radio>
          <a-radio value="1">学院</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item>
        <a-button
          html-type="submit"
          style="width: 200px; margin: auto"
          type="primary"
          >登录
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import { reactive } from "vue";
import ACCESS_ENUM from "@/access/accessEnum";
import store from "@/store";
import { useRouter } from "vue-router";
import { StudentControllerService } from "../../generated";
import { message } from "ant-design-vue";

const loginRequest = reactive({
  userAccount: "",
  password: "",
  userRole: "",
});

const router = useRouter();

const handleLogin = async () => {
  const res = await StudentControllerService.loginUsingPost(loginRequest);
  if (res.success === true) {
    await store.dispatch("user/getLoginUser", {
      userAccount: res.data.userAccount,
      userRole: res.data.userRole,
      userId: res.data.id,
    });
    sessionStorage.setItem("LoginUserAccount", res.data.userAccount);
    sessionStorage.setItem("LoginUserRole", res.data.userRole);
    sessionStorage.setItem("LoginUserId", res.data.id);
    router.push({
      path: "/profile/index",
      replace: true,
    });
  } else {
    message.error("登陆失败，", res.message);
  }
};
</script>

<style scoped></style>
